package com.android.nursehelper.utils

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.StringUtils

/**
 * 跳转到自启动管理
 */
object AutoFiringUtils {
    private const val json =
        "{\"360\":[\"com.yulong.android.coolsafe/.ui.activity.autorun.AutoRunListActivity\",\"com.yulong.android.coolsafe\"],\"F\":[\"com.gionee.softmanager/.MainActivity\",\"com.gionee.softmanager\"],\"HUAWEI\":[\"com.huawei.systemmanager/.startupmgr.ui.StartupNormalAppListActivity\",\"com.huawei.systemmanager/.appcontrol.activity.StartupAppControlActivity\",\"com.huawei.systemmanager/.optimize.process.ProtectActivity\",\"com.huawei.systemmanager/.optimize.bootstart.BootStartActivity\",\"com.huawei.systemmanager\"],\"Meizu\":[\"com.meizu.safe/.permission.SmartBGActivity\",\"com.meizu.safe/.permission.PermissionMainActivity\",\"com.meizu.safe\"],\"OPPO\":[\"com.coloros.safecenter/.startupapp.StartupAppListActivity\",\"com.coloros.safecenter/.permission.startup.StartupAppListActivity\",\"com.oppo.safe/.permission.startup.StartupAppListActivity\",\"com.coloros.oppoguardelf/com.coloros.powermanager.fuelgaue.PowerUsageModelActivity\",\"com.coloros.safecenter/com.coloros.privacypermissionsentry.PermissionTopActivity\",\"com.coloros.safecenter\",\"com.oppo.safe\",\"com.coloros.oppoguardelf\"],\"Xiaomi\":[\"com.miui.securitycenter/com.miui.permcenter.autostart.AutoStartManagementActivity\",\"com.miui.securitycenter\"],\"asus\":[\"com.asus.mobilemanager/.MainActivity\",\"com.asus.mobilemanager\"],\"coolpad\":[\"com.yulong.android.security/com.yulong.android.seccenter.tabbarmain\",\"com.yulong.android.security\"],\"htc\":[\"com.htc.pitroad/.landingpage.activity.LandingPageActivity\",\"com.htc.pitroad\"],\"lenovo\":[\"com.lenovo.security/.purebackground.PureBackgroundActivity\",\"com.lenovo.security\"],\"letv\":[\"com.letv.android.letvsafe/.AutobootManageActivity\",\"com.letv.android.letvsafe/.BackgroundAppManageActivity\",\"com.letv.android.letvsafe\"],\"oneplus\":[\"com.oneplus.security/.chainlaunch.view.ChainLaunchAppListActivity\",\"com.oneplus.security\"],\"samsung\":[\"com.samsung.android.sm_cn/com.samsung.android.sm.ui.ram.AutoRunActivity\",\"com.samsung.android.sm_cn/com.samsung.android.sm.ui.appmanagement.AppManagementActivity\",\"com.samsung.android.sm_cn/com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity\",\"com.samsung.android.sm_cn/.ui.ram.RamActivity\",\"com.samsung.android.sm_cn/.app.dashboard.SmartManagerDashBoardActivity\",\"com.samsung.android.sm/com.samsung.android.sm.ui.ram.AutoRunActivity\",\"com.samsung.android.sm/com.samsung.android.sm.ui.appmanagement.AppManagementActivity\",\"com.samsung.android.sm/com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity\",\"com.samsung.android.sm/.ui.ram.RamActivity\",\"com.samsung.android.sm/.app.dashboard.SmartManagerDashBoardActivity\",\"com.samsung.android.lool/com.samsung.android.sm.ui.battery.BatteryActivity\",\"com.samsung.android.sm_cn\",\"com.samsung.android.sm\"],\"smartisanos\":[\"com.smartisanos.security/.invokeHistory.InvokeHistoryActivity\",\"com.smartisanos.security\"],\"ulong\":[\"com.yulong.android.coolsafe/.ui.activity.autorun.AutoRunListActivity\",\"com.yulong.android.coolsafe\"],\"vivo\":[\"com.iqoo.secure/.ui.phoneoptimize.BgStartUpManager\",\"com.iqoo.secure/.safeguard.PurviewTabActivity\",\"com.vivo.permissionmanager/.activity.BgStartUpManagerActivity\",\"com.iqoo.secure\",\"com.vivo.permissionmanager\"],\"zte\":[\"com.zte.heartyservice/.autorun.AppAutoRunManager\",\"com.zte.heartyservice\"]}"

    private val manufacturerMap = mutableMapOf<String, MutableList<String>>()

    private fun initManufacturerMap() {
        if (manufacturerMap.isEmpty()) {
            val jsonObject = JSON.parseObject(json)
            val keys = jsonObject.keys
            for (key in keys) {
                val jsonArray = jsonObject.getJSONArray(key)
                val list = mutableListOf<String>()
                manufacturerMap[key] = list
                for (i in 0 until jsonArray.size) {
                    list.add(jsonArray.getString(i))
                }
            }
        }
    }

    fun start(context: Context) {
        initManufacturerMap()
        val currentManufacturer = Build.MANUFACTURER
        var isFind = false
        manufacturerMap.forEach { (key, list) ->
            if (StringUtils.equalsIgnoreCase(currentManufacturer, key)) {
                list.forEach { option ->
                    try {
                        var intent: Intent? = null
                        if (option.contains("/")) {
                            intent = Intent()
                            intent?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            intent?.component = ComponentName.unflattenFromString(option)
                        } else {
                            intent = context.packageManager.getLaunchIntentForPackage(option)
                        }
                        context.startActivity(intent)
                        isFind = true
                    } catch (e: java.lang.Exception) {

                    }
                }
            }
        }
        if (!isFind) { //兼容方案
            var intent: Intent
            try {
                intent = Intent()
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                intent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
                intent.data = Uri.fromParts("package", context.packageName, null)
                context.startActivity(intent)
            } catch (e2: Exception) {
                e2.printStackTrace()
                intent = Intent("android.settings.SETTINGS")
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                context.startActivity(intent)
            }
        }
    }
}